package cn.zc.controller;

import cn.zc.constant.JwtClaimsConstant;
import cn.zc.entity.StudentVo;
import cn.zc.entity.User;
import cn.zc.mapper.LoginMapper;
import cn.zc.mapper.StudentMapper;
import cn.zc.properties.JwtProperties;
import cn.zc.result.Result;
import cn.zc.service.LoginService;
import cn.zc.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/login")
@Slf4j
//@CrossOrigin(origins = "*", methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT, RequestMethod.DELETE, RequestMethod.PATCH, RequestMethod.OPTIONS})
public class LoginController {
    @Autowired
    private LoginService loginService;
    @Autowired
    private LoginMapper loginMapper;
    @Autowired
    private StudentMapper studentMapper;
    private JwtProperties jwtProperties;

    @GetMapping("/test")
    public Result test(String id, String password) {
        User build = User.builder().id(id).mypassword(password).build();
        User login = loginMapper.login(build);
        int type =login.getType();
        if (login.getIsonelogin() == 1){
            //第一次登录，生成uuid
            String token = UUID.randomUUID().toString();
            StudentVo studentVo = new StudentVo();
            studentVo.setType(type);
            studentVo.setToken(token);
            studentMapper.setuuid(login.getId(),token);
            studentMapper.setOnelogin(login.getId(),0);
            // 获取当前日期和时间
            LocalDateTime now = LocalDateTime.now();
            // 格式化日期时间
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            String formattedDateTime = now.format(formatter);
            loginMapper.insertLoginLog(id,formattedDateTime);
            return Result.success(studentVo);
        }else {
            //不是第一次登录，那么一定携带这token，由于过滤器会拦截，在过滤器中会验证token
            StudentVo studentVo = new StudentVo();
            studentVo.setType(type);//TODO 登录成功0代表学生，1代表管理员
            studentVo.setIsonelogin(0);//是否是第一次登录
            // 获取当前日期和时间
            LocalDateTime now = LocalDateTime.now();
            // 格式化日期时间
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            String formattedDateTime = now.format(formatter);
            loginMapper.insertLoginLog(id,formattedDateTime);
            return Result.success(studentVo);
        }
//        Map<String, Object> claims = new HashMap<>();
//        claims.put(JwtClaimsConstant.Student_ID, build.getId());
//        String token = JwtUtil.createJWT(
//                jwtProperties.getStudentSecretKey(),
//                jwtProperties.getStudentTtl(),
//                claims);

    }
//    public Result test(String id, String password) {
////        System.out.println(mylatitude+mylongitude);
//        User build = User.builder().id(id).mypassword(password).build();
//        UserContext.setUser(build);
//        return Result.success(0);
//    }
//    @GetMapping("https://restapi.amap.com/v3/bus/stopid?key=<bfcc1198b984ef88992878b7328a46c5>")
//    public Result testbaidu(String id, String password,String uuid,String mylatitude,String mylongitude) {
//        System.out.println(mylatitude+mylongitude);
//        User build = User.builder().id(id).mypassword(password).build();
//        Result login = loginService.login(build,uuid,mylatitude,mylongitude);
//        return login;
//    }
}
